home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / zfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  9.0 KB  |  336 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfont.c */
  20. /* Font operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gzstate.h"        /* must precede gxdevice */
  25. #include "gxdevice.h"        /* must precede gxfont */
  26. #include "gschar.h"
  27. #include "gxfont.h"
  28. #include "gxfdir.h"
  29. #include "gxcache.h"
  30. #include "alloc.h"
  31. #include "bfont.h"
  32. #include "dict.h"
  33. #include "iname.h"
  34. #include "packed.h"
  35. #include "save.h"
  36. #include "state.h"
  37. #include "store.h"
  38.  
  39. /* Imported operators */
  40. extern int zcleartomark(P1(os_ptr));
  41. #ifdef AMIGA
  42. extern void gs_purge_fm_pair(gs_font_dir *, cached_fm_pair *, int);
  43. #endif
  44.  
  45. /* Forward references */
  46. private int font_param(P2(os_ptr, gs_font **));
  47. private int make_font(P3(os_ptr, const gs_matrix *, const ref *));
  48. private void make_uint_array(P3(os_ptr, const uint *, int));
  49.  
  50. /* The (global) font directory */
  51. gs_font_dir *ifont_dir = 0;        /* needed for buildfont */
  52.  
  53. /* Names of system-known keys in font dictionaries: */
  54. ref name_FontMatrix;            /* needed for buildfont */
  55. ref name_FID;
  56. private ref name_OrigFont;        /* needed for scalefont/makefont */
  57. private ref name_ScaleMatrix;        /* ditto */
  58.  
  59. /* Initialize the font operators */
  60. private void
  61. zfont_init(void)
  62. {    static const names_def fnd[] = {
  63.  
  64.     /* Create the names of the standard elements of */
  65.     /* a font dictionary. */
  66.        { "FontMatrix", &name_FontMatrix },
  67.        { "FID", &name_FID },
  68.        { "OrigFont", &name_OrigFont },
  69.        { "ScaleMatrix", &name_ScaleMatrix },
  70.  
  71.     /* Mark the end of the initialized name list. */
  72.        names_def_end
  73.     };
  74.  
  75.     ifont_dir = gs_font_dir_alloc(&alloc_memory_procs);
  76.     init_names(fnd);
  77. }
  78.  
  79. /* <font> <scale> scalefont <new_font> */
  80. int
  81. zscalefont(register os_ptr op)
  82. {    int code;
  83.     float scale;
  84.     gs_matrix mat;
  85.     if ( (code = num_params(op, 1, &scale)) < 0 ) return code;
  86.     if ( (code = gs_make_scaling(scale, scale, &mat)) < 0 ) return code;
  87.     return make_font(op, &mat, NULL);
  88. }
  89.  
  90. /* <font> <matrix> makefont <new_font> */
  91. int
  92. zmakefont(register os_ptr op)
  93. {    int code;
  94.     gs_matrix mat;
  95.     if ( (code = read_matrix(op, &mat)) < 0 ) return code;
  96.     return make_font(op, &mat, op);
  97. }
  98.  
  99. /* <font> setfont - */
  100. int
  101. zsetfont(register os_ptr op)
  102. {    gs_font *pfont;
  103.     int code = font_param(op, &pfont);
  104.     if ( code < 0 || (code = gs_setfont(igs, pfont)) < 0 )
  105.       return code;
  106.     istate.font = *op;
  107.     pop(1);
  108.     return code;
  109. }
  110.  
  111. /* - currentfont <font> */
  112. int
  113. zcurrentfont(register os_ptr op)
  114. {    push(1);
  115.     *op = istate.font;
  116.     return 0;
  117. }
  118.  
  119. /* - cachestatus <mark> <bsize> <bmax> <msize> <mmax> <csize> <cmax> <blimit> */
  120. int
  121. zcachestatus(register os_ptr op)
  122. {    uint status[7];
  123.     gs_cachestatus(ifont_dir, status);
  124.     push(7);
  125.     make_uint_array(op - 6, status, 7);
  126.     return 0;
  127. }
  128.  
  129. /* <blimit> setcachelimit - */
  130. int
  131. zsetcachelimit(register os_ptr op)
  132. {    long limit;
  133.     check_type(*op, t_integer);
  134.     limit = op->value.intval;
  135.     if ( (ulong)limit > max_uint )    /* also covers limit < 0 */
  136.         return_error(e_rangecheck);
  137.     gs_setcachelimit(ifont_dir, (uint)limit);
  138.     pop(1);
  139.     return 0;
  140. }
  141.  
  142. /* <mark> <size> <lower> <upper> setcacheparams - */
  143. int
  144. zsetcacheparams(register os_ptr op)
  145. {    uint params[2];
  146.     int i, code;
  147.     for ( i = 0; i < 2 && !r_has_type(op - i, t_mark) ; i++ )
  148.        {    long parm;
  149.         check_type(op[-i], t_integer);
  150.         parm = op[-i].value.intval;
  151.         if ( (ulong)parm > max_uint )    /* covers parm < 0 */
  152.             return_error(e_rangecheck);
  153.         params[i] = parm;
  154.        }
  155.     switch ( i )
  156.        {
  157.     case 2:
  158.         if ( (code = gs_setcachelower(ifont_dir, params[1])) < 0 )
  159.             return code;
  160.     case 1:
  161.         if ( (code = gs_setcacheupper(ifont_dir, params[0])) < 0 )
  162.             return code;
  163.     case 0: ;
  164.        }
  165.     return zcleartomark(op);
  166. }
  167.  
  168. /* - currentcacheparams <mark> <size> <lower> <upper> */
  169. int
  170. zcurrentcacheparams(register os_ptr op)
  171. {    uint params[2];
  172.     params[0] = gs_currentcachelower(ifont_dir);
  173.     params[1] = gs_currentcacheupper(ifont_dir);
  174.     push(3);
  175.     make_tv(op - 2, t_mark, intval, 0);
  176.     make_uint_array(op - 1, params, 2);
  177.     return 0;
  178. }
  179.  
  180. /* ------ Initialization procedure ------ */
  181.  
  182. op_def zfont_op_defs[] = {
  183.     {"0currentfont", zcurrentfont},
  184.     {"2makefont", zmakefont},
  185.     {"2scalefont", zscalefont},
  186.     {"1setfont", zsetfont},
  187.     {"0cachestatus", zcachestatus},
  188.     {"1setcachelimit", zsetcachelimit},
  189.     {"1setcacheparams", zsetcacheparams},
  190.     {"0currentcacheparams", zcurrentcacheparams},
  191.     op_def_end(zfont_init)
  192. };
  193.  
  194. /* ------ Subroutines ------ */
  195.  
  196. /* Validate a font parameter. */
  197. private int
  198. font_param(os_ptr fp, gs_font **pfont)
  199. {    /* Check that fp is a read-only dictionary, */
  200.     /* and that it has a FID entry. */
  201.     ref *pid;
  202.     check_type(*fp, t_dictionary);
  203.     if ( dict_find(fp, &name_FID, &pid) <= 0 )
  204.         return_error(e_invalidfont);
  205.     *pfont = pid->value.pfont;
  206.     if ( *pfont == 0 )
  207.         return_error(e_invalidfont); /* unregistered font */
  208.     return 0;
  209. }
  210.  
  211. /* Add the FID entry to a font dictionary. */
  212. int
  213. add_FID(ref *fp /* t_dictionary */,  gs_font *pfont)
  214. {    ref fid;
  215.     make_tv_new(&fid, t_fontID, pfont, pfont);
  216.     return dict_put(fp, &name_FID, &fid);
  217. }
  218.  
  219. /* Make a transformed font (common code for makefont/scalefont). */
  220. private int
  221. make_font(os_ptr op, const gs_matrix *pmat, const ref *pmref)
  222. {    os_ptr fp = op - 1;
  223.     gs_font *oldfont, *newfont, *ffont;
  224.     int code;
  225.     if ( (code = font_param(fp, &oldfont)) < 0 ||
  226.          (code = gs_makefont(ifont_dir, oldfont, pmat,
  227.                  &newfont, &ffont)) < 0
  228.        )
  229.       return code;
  230.     /* Check whether the scaled font was already cached. */
  231.     if ( newfont->client_data == 0 )    /* not in the cache */
  232.     {    ref newdict, newdata, newmat;
  233.         ref mref;
  234. #define font_data_refs (sizeof(font_data) / sizeof(ref))
  235.         uint data_len = font_data_refs + 6;
  236.         uint dlen = dict_maxlength(fp);
  237.         /* Ensure room for FontID, OrigFont, ScaleMatrix. */
  238.         uint mlen = dict_length(fp) + 3;
  239.         if ( pmref == NULL )
  240.         {    /* Prepare to create the matrix implied by scalefont. */
  241.             data_len += 6;
  242.         }
  243.         if ( dlen < mlen )
  244.             dlen = mlen;
  245.         if ( (code = dict_create(dlen, &newdict)) < 0 ||
  246.              (code = dict_copy(fp, &newdict)) < 0 ||
  247.              (code = alloc_array(&newdata, a_all, data_len, "make_font")) < 0
  248.            )
  249.           return code;
  250.         if ( pmref == NULL )
  251.         {    /* Create the scaling matrix now. */
  252.             data_len -= 6;
  253.             refcpy_to_new(newdata.value.refs + data_len,
  254.                       (const ref *)pmat, 6);
  255.             r_set_size(&newdata, data_len);
  256.             make_array(&mref, a_readonly, 6,
  257.                    newdata.value.refs + data_len);
  258.             pmref = &mref;
  259.         }
  260.         make_array(&newmat, a_readonly, 6, newdata.value.refs + font_data_refs);
  261. #undef font_data_refs
  262.         if ( (code = dict_put(&newdict, &name_FontMatrix, &newmat)) < 0 ||
  263.              (code = dict_put(&newdict, &name_OrigFont, fp)) < 0 ||
  264.              (pmref != NULL && (code = dict_put(&newdict, &name_ScaleMatrix, pmref)) < 0) ||
  265.              (code = add_FID(&newdict, newfont)) < 0
  266.            )
  267.             return code;
  268.         newfont->client_data = (char *)newdata.value.refs;
  269.         *(font_data *)newdata.value.refs =
  270.             *(font_data *)(oldfont->client_data);
  271.         ((font_data *)newdata.value.refs)->dict = newdict;
  272.         *(gs_matrix *)newmat.value.refs = newfont->FontMatrix;
  273.         r_clear_attrs(dict_access_ref(&newdict), a_write);
  274.     }
  275.     *fp = ((font_data *)(newfont->client_data))->dict;
  276.     if ( ffont )
  277.       { /****** SHOULD DECREMENT REFCT ******/
  278.       }
  279.     pop(1);
  280.     return 0;
  281. }
  282.  
  283. /* Convert an array of (unsigned) integers to stack form. */
  284. private void
  285. make_uint_array(register os_ptr op, const uint *intp, int count)
  286. {    int i;
  287.     for ( i = 0; i < count; i++, op++, intp++ )
  288.         make_int(op, *intp);
  289. }
  290.  
  291. /* Remove scaled font and character cache entries that would be */
  292. /* invalidated by a restore. */
  293. void
  294. font_restore(const alloc_save *save)
  295. {    gs_font_dir *pdir = ifont_dir;
  296.     if ( pdir == 0 )        /* never initialized */
  297.         return;
  298.  
  299.     /* Purge original (unscaled) fonts. */
  300.  
  301.     {    gs_font *pfont;
  302. otop:        for ( pfont = pdir->orig_fonts; pfont != 0;
  303.               pfont = pfont->next
  304.             )
  305.         { if ( alloc_is_since_save((char *)pfont, save) )
  306.            { gs_purge_font(pfont); goto otop; }
  307.         }
  308.     }
  309.  
  310.     /* Purge scaled fonts. */
  311.  
  312.     {    gs_font *pfont;
  313. top:        for ( pfont = pdir->scaled_fonts; pfont != 0;
  314.               pfont = pfont->next
  315.             )
  316.         { if ( alloc_is_since_save((char *)pfont, save) )
  317.            { gs_purge_font(pfont); goto top; }
  318.         }
  319.     }
  320.  
  321.     /* Purge xfonts. */
  322.  
  323.     {    cached_fm_pair *pair;
  324.         uint n;
  325.         for ( pair = pdir->fmcache.mdata, n = pdir->fmcache.mmax;
  326.               n > 0; pair++, n--
  327.             )
  328.         {    if ( !fm_pair_is_free(pair) &&
  329.                  pair->xfont != 0 &&
  330.                  alloc_is_since_save((char *)pair->xfont, save)
  331.                )
  332.               gs_purge_fm_pair(pdir, pair, 1);
  333.         }
  334.     }
  335. }
  336.